package singleton;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author：LJ
 * @Description：
 * @Date: 2017/11/24
 * @Modified By:
 */
public class EmperorExtend {
    //最多能定义几个对象
    private static int max = 5;
    private static List<String> nameList = new ArrayList<String>();
    private static List<EmperorExtend> emperorList = new ArrayList<EmperorExtend>();
    private static Integer currentIndex;

    static {
        for (int i = 0; i < max; i++) {
            emperorList.add(new EmperorExtend("黄帝" + i));
        }
    }

    private EmperorExtend() {
    }

    private EmperorExtend(String name) {
        nameList.add(name);
    }

    public static EmperorExtend getInstance() {
        Random random = new Random();
        currentIndex = random.nextInt(max);
        return emperorList.get(currentIndex);
    }

    public static void say() {
        System.out.println(nameList.get(currentIndex)+"有事启奏,无事退朝");
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            EmperorExtend emperor = EmperorExtend.getInstance();
            emperor.say();
        }
    }
}
